-- opts
local g = vim.g
local opt = vim.opt

g.mapleader = ' '
g.transparency = true

opt.confirm = true;


-- Indenting
opt.expandtab = true
opt.shiftwidth = 2
opt.smartindent = true
opt.tabstop = 2
opt.softtabstop = 2

-- Numbers
opt.number = true
opt.numberwidth = 2
opt.fillchars = { eob = " " }
opt.cursorline = true
opt.relativenumber = true

opt.clipboard = "unnamedplus"

opt.showmode = false
opt.laststatus = 3     -- global statusline
-- disable nvim intro
opt.shortmess:append "sI"

-- search
opt.ignorecase = true
opt.smartcase = true
-- vim.cmd[[colorscheme blue]]
vim.opt.termguicolors = true

opt.signcolumn = "yes"
opt.splitbelow = true
opt.splitright = true
opt.termguicolors = true
opt.timeoutlen = 400
opt.undofile = true
opt.mouse = "a"

-- keymap
local map = vim.keymap.set
map('n', '<leader>e', "<cmd>Ex<cr>", { desc = 'explorer' })
map('n', ';', ":", { desc = 'cmdline mode' })
-- Move Lines
map("n", "<A-j>", "<cmd>m .+1<cr>==", { desc = "Move Down" })
map("n", "<A-k>", "<cmd>m .-2<cr>==", { desc = "Move Up" })
map("i", "<A-j>", "<esc><cmd>m .+1<cr>==gi", { desc = "Move Down" })
map("i", "<A-k>", "<esc><cmd>m .-2<cr>==gi", { desc = "Move Up" })
map("v", "<A-j>", ":m '>+1<cr>gv=gv", { desc = "Move Down" })
map("v", "<A-k>", ":m '<-2<cr>gv=gv", { desc = "Move Up" })

map({ 'n' }, 'x', '"_x', {desc = 'delete char but not pollute system clipboard'}) -- to aovid pollute system clipboard
map('n', '<Esc>', '<cmd> noh <CR>', { desc = 'clear highlight' })
-- Esc can be conflicted with lazygit
map({ 't' }, '<Esc>', '<Esc><C-\\><C-n>')
map({ 't' }, '<C-x>', '<C-\\><C-n>')
map('i', '<C-l>', '<right>', { desc = "right" })
map('i', '<C-h>', '<Left>', { desc = "left" })
map('i', '<C-j>', '<esc>j^i', { desc = 'edit line below' })
map('i', '<C-k>', '<esc>k^i', { desc = 'insert line above' })
map({ 'n' }, '<C-h>', '<C-w><C-h>')
map({ 'n' }, '<C-l>', '<C-w><C-l>')
map({ 'n' }, '<C-j>', '<C-w><C-j>')
map({ 'n' }, '<C-k>', '<C-w><C-k>')
map({ 't' }, '<C-h>', '<C-\\><C-n><C-w><C-h>')
map({ 't' }, '<C-l>', '<C-\\><C-n><C-w><C-l>')
map({ 't' }, '<C-j>', '<C-\\><C-n><C-w><C-j>')
map({ 't' }, '<C-k>', '<C-\\><C-n><C-w><C-k>')
map({ 'v' }, '>', '>gv', { desc = "continue to indent" })
map({ 'v' }, '<', '<gv', { desc = "continue to indent" })

map('n', '<leader>x', "<cmd> bd <CR>", { desc = 'close current buffer' })
map('n', '<leader>bd', "<cmd> bufdo bd <CR>", { desc = 'close all buffers' })
map('n', '<Tab>', "<cmd> bn <CR>", { desc = 'next buffer' })
map('n', '<S-Tab>', "<cmd> bp <CR>", { desc = 'prev buffer' })
map('n', '<leader>bn', "<cmd> enew <CR>", { desc = 'new buffer' })
map('n', '<leader>bw', "<cmd>wa<CR>", { desc = 'write all buffers' })
map('n', '<leader>bl', "<cmd>buffers<CR>", { desc = 'list all buffers' })
map('n', '<leader>1', "<cmd>b 1<CR>", { desc = 'buffer 1' })
map('n', '<leader>2', "<cmd>b 2<CR>", { desc = 'buffer 2' })
map('n', '<leader>3', "<cmd>b 3<CR>", { desc = 'buffer 3' })
map('n', '<leader>4', "<cmd>b 4<CR>", { desc = 'buffer 4' })
map('n', '<leader>5', "<cmd>b 5<CR>", { desc = 'buffer 5' })

-- vim.api.nvim_create_autocmd("VimEnter", {
--   callback = function(event)
--     -- autocmd
--     vim.cmd 'Ex'
--   end,
-- })
--
--


local function augroup(name)
  return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true })
end


-- make it easier to close man-files when opened inline
vim.api.nvim_create_autocmd("FileType", {
  group = augroup("close_with_q"),
  pattern = {
    "PlenaryTestPopup",
    "help",
    "lspinfo",
    "notify",
    "qf",
    "query",
    "spectre_panel",
    "startuptime",
    "tsplayground",
    "neotest-output",
    "checkhealth",
    "neotest-summary",
    "neotest-output-panel",
    "netrw",
    "man",
    "terminal"
  },
  callback = function(event)
    vim.bo[event.buf].buflisted = false
    vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
  end,
})

vim.api.nvim_create_autocmd("TextYankPost", {
  group = augroup("highlight_yank"),
  callback = function()
    vim.highlight.on_yank()
  end,
})


vim.api.nvim_create_autocmd("FileType", {
  group = augroup("wrap_spell"),
  pattern = { "gitcommit", "markdown" },
  callback = function()
    vim.opt_local.wrap = true
    vim.opt_local.spell = true
  end,
})


-- Auto create dir when saving a file, in case some intermediate directory does not exist
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
  group = augroup("auto_create_dir"),
  callback = function(event)
    if event.match:match("^%w%w+:[\\/][\\/]") then
      return
    end
    local file = vim.uv.fs_realpath(event.match) or event.match
    vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
  end,
})

